/* ----------------------------------------------------------------------------------------------------
* 个人中心
* ----------------------------------------------------------------------------------------------------
*/

/* 左侧菜单 */
function memberHandler()
{
	$(".subMenu dt").click(function(){
		if ($(this).hasClass("collapse")){
			$(this).removeClass("collapse").nextAll("dd").show();
		}else{
			$(this).addClass("collapse").nextAll("dd").hide();
		}
	});
	$(".subMenu a").click(function(){
		$(".subMenu").children("dd").removeClass("currentMenu");
		$(this).parent().addClass("currentMenu");
	});
}

/* 充值 */
function paycountnow(){
//	$.post(APP+"/Member/regcheck",
//	{},
//	function(data){
//		if(data=="1"){
			addDialogbox("#mbpaycountnow",BEATNAME + "充值",10001,768);
			//quitScreenMask();
			$.ajax({
				url: APP+"/Member/mb_count_single/",
				cache: false,
				success: function(msg){
					$("#mbpaycountnow .dialogboxContent").addClass("regWrap");
					$("#mbpaycountnow .dialogboxContent").prepend(msg);
				}
			});
//		}else{
//			addDialogbox("#check_dl","您的"+BEATNAME+"充足，无需充值。",10010,302);
//			msg=''+
//			'<div class="tc">'+
//			'<span class="button">'+
//			'<span class="first-child">'+
//			'<button type="button" class="f12" onclick="check_dl_ok()">我知道了,返回</button>'+
//			'</span>'+
//			'</span>'+
//			'</div>';
//			$("#check_dl .dialogboxContent").html(msg);
//		}
//	});
}

function check_dl_ok(){
	removeDialogbox("#check_dl");
}


function inimember()
{
	var islogin;
	var mb_id;
	var mb_nickname;
	var mb_key;
	var content;
	islogin=getCookie('islogin');
	
	mb_id=getCookie('mb_id');
	//mb_key=getCookie('mb_key');
	//setTimeout('imiajax()',10000);//初始化ajax	
	if(!islogin || !mb_id){
		
		content=
		'<form id="loginform" autocomplete="off">'+
        '<div style="margin-left:20px;">'+
        '<div style="float:left;">邮箱：<input type="text" style=" height:20px;width:80px;" id="userID" name="username" class="edit_input"></div>'+
        '<div style="float:left; margin-left:10px;">密&nbsp;&nbsp;码：<input type="password" style="width:80px; height:20px;" id="userPW" name="password" class="edit_input"></div>'+
        '<input type="hidden" name="loginmark" id="loginmark" value="L">'+
        '<div class="yuns_cl"></div></div>'+
        '<div style=" margin-top:10px;">'+
        '<div class="yuns_denglu fl"><a href="javascript:indexlogin();"></a></div>'+
        '<div class="yuns_zhuce fl" style="margin:0 10px;"><a id="signup" href="'+APP+'/User/register" title="免费注册" style="color:#f60;"></a></div>'+
        '<div class="yuns_zhifub fl"><a href="javascript:forgot();"></a></div>'+ 
        '<div class="yuns_cl"></div></div>'+
        '<div id="tips"></div></form>';
		$("#accountInfo").html(content);

	}else{
		$.ajax({
			url: APP+"/User/inimember/",
			cache: false,
			type: "POST",
			dataType: "json",
			error: function (XMLHttpRequest, textStatus, errorThrown){logout();},
			success: function(data){
				//alert(data.nickname);
				//alert(data[0]);
				if(data.newNum>0){
					var newscontent='<span class="yuns_mg"><img src="'+IMG_PATH+'/news.gif" /> <a id="userNews" href="'+APP+'/Member/index/void/mb_message/" title="您有新短信">新短信('+data.newNum+'条)</span>';
				}else{
					var newscontent='';
				}
				content=
				'<div style="margin-left:20px; line-height:25px;">'+
				'您好，<span class="yuns_mg">'+data.nickname+'</span>，'+
				'<span class="yuns_mg"><a href="'+APP+'/Member" title="到个人中心">个人中心</a></span>'+
				/*'<a href="javascript:copy_code();" title="推荐好友，奖励'+POINTBNAME+'"><img width="52" height="19" alt="推荐好友，奖励'+POINTBNAME+'" src="'+IMG_PATH+'/member/inviteBuddy.gif" /></a>'+*/
				newscontent+
				'<span class="yuns_mg"><a href="javascript:logout();" title="安全退出">退出</a></span></div>'+
				'<div id="accountBalanceWrap" class="dropMenu">'+
				'<h3 id="checkMyAccount" title="查看账户信息" style="width:70px;">账户信息<span class="f10">▼</span></h3>'+
				'<div id="accountBalance" style="width:220px;display:block;">'+
				'</div>'+
				'<div class="dropMenuShadow" style="height:100px;"></div>'+
				'</div>';

				$("#accountInfo").html(content);
				useraccount();
				updateuser();

				$("#checkMyAccount").hover(
					function(){
						var content;
						var isgetPointB=getCookie('isgetPointB');
						var PointB_btn;
						var isgetPointC=getCookie('isgetPointC');
						var PointC_btn;
	
						if(isgetPointB==0){
							PointB_btn = '<a id="getPointB" href="javascript:getPointB();" title="打卡领取'+POINTBNAME+'" class="conutType getPointB"></a>';
						}else{
							PointB_btn = '<a id="gotPointB" href="javascript:void(0);"  class="conutType getPointC"></a>';
						}						
						$("#accountBalance").html(loading);
						$.ajax({
							type: "POST",
							url:APP+"/User/getcount/",
							dataType: "json",
							success: function(data){
								if(data.islogin=='0'){
									logout();//如果服务里转回1刚表示服务器里已没有这个用户的session
								}else{
									content='<table style="line-height:25px; margin:5px 0 0 20px; width:240px;">'+
											'<tr>'+
											'<td width="20%"><span id="balanceA" class="conutType"></span></td>'+
											'<td width="10%"></td>'+
											'<td width="50px"><strong class="fl keyword">'+data.a_point+'</strong></td>'+
											'<td width="20px"><a id="getPointA" href="javascript:paycountnow();" title="充值获取'+BEATNAME+'" class="conutType getPointA"></a></td>'+
											'<tr>'+
											'<td width="20%"><span id="balanceB" class="conutType"></span></td>'+
											'<td width="10%"></td>'+
											'<td width="50px"><strong class="fl keyword">'+data.b_point+'</strong></td>'+
											'<td width="20px">'+PointB_btn+'</td>'+
											'<tr>'+
											'</table>';
									$("#accountBalance").html(content);
								}
							}
						});
					},
					
					function(){
					}
					);

			}
		});
	}
}
function useraccount(){
	var content;
	var isgetPointB=getCookie('isgetPointB');
	var PointB_btn;
	var isgetPointC=getCookie('isgetPointC');
	var PointC_btn;

	if(isgetPointB==0){
		PointB_btn = '<a id="getPointB" href="javascript:getPointB();" title="打卡领取'+POINTBNAME+'" class="conutType getPointB"></a>';
	}else{
		PointB_btn = '<a id="gotPointB" href="javascript:void(0);"  class="conutType getPointC"></a>';
	}
	
	$("#accountBalance").html(loading);
	$.ajax({
		type: "POST",
		url:APP+"/User/getcount/",
		dataType: "json",
		success: function(data){
			if(data.islogin=='0'){
				logout();//如果服务里转回1刚表示服务器里已没有这个用户的session
			}else{
				content='<table style="line-height:25px; margin:5px 0 0 20px; width:240px;">'+
						'<tr>'+
						'<td width="20%"><span id="balanceA" class="conutType"></span></td>'+
						'<td width="10%"></td>'+
						'<td width="50px"><strong class="fl keyword">'+data.a_point+'</strong></td>'+
						'<td width="20px"><a id="getPointA" href="javascript:paycountnow();" title="充值获取'+BEATNAME+'" class="conutType getPointA"></a></td>'+
						'<tr>'+
						'<td width="20%"><span id="balanceB" class="conutType"></span></td>'+
						'<td width="10%"></td>'+
						'<td width="50px"><strong class="fl keyword">'+data.b_point+'</strong></td>'+
						'<td width="20px">'+PointB_btn+'</td>'+
						'<tr>'+
						'</table>';
				$("#accountBalance").html(content);
			}
		}
	});
}

function tlsalert(txt){
	nbalert(txt,"提示");
}

function nbalert(txt,title){
	addDialogbox("#nbalertox",title,10001,402);
	msg=txt+'<br /><br />'+
	'<div class="tc">'+
	'<span class="button">'+
	'<span class="first-child">'+
	'<button type="button" class="f12" onclick="removeDialogbox(\'#nbalertox\');">我知道了，返回</button>'+
	'</span>'+
	'</span>'+
	'</div>';
	$("#nbalertox .dialogboxContent").html(msg);
}

/*
//玩点
function getPointC(){
	$.post(APP+"/Member/ajaxDoPuncher",{},
		function(d,success){
			var info=eval(d);
			data = info[0];
			times = info[1];
			
			var ruleOfPointC= '<br /><br />'+
			'玩点打卡规则：'+
			'<ul class="outside disc" style="margin-left:26px;">'+
			'<li>玩点余额不足10000时，每次打卡领取1000玩点；</li>'+
			'<li>玩点余额超过10000时，每次打卡领取200玩点；</li>'+
			'<li>玩点余额不足1000时，每次打卡领取1000玩点，每天4次哦。</li>'+
			'</ul>';

			if(data==0){//$exit_nologon = 0;$exit_ok = 1;$exit_done = 2;$exit_fail = 3;
				tlsalert("对不起，您尚未登录。");
			}else if(data==1){
				//alert("打卡成功!");
				//mbsimajax(APP+"/Member/mb_index","mbAjaxShow");
				tlsalert("今天第"+times+"次打卡成功！");
			}else if(data==2){
				tlsalert("今天已经打过卡了，请明天再来。");
			}else if(data==3){
				tlsalert("操作失败，请重新打卡。");
			}else if(data==4){
				tlsalert("为了您的账户安全，请先进行\"帐户安全认证\"!");
			}else if(data==5){
				tlsalert("本次打卡不符合玩点打卡规则。"+ruleOfPointC);
			}else{
				tlsalert("非法操作!");
			}
		}
	);
}
*/

//赠送打卡ajax 操作
function getPointB(){
	//alert("OK!");
	$.post(APP+"/Member/ajaxDoExpoint",{},
		function(data,success){ ///1充值成功 2打过卡 3充值失败 4未通过安全验证
		//alert(data);
		if(data == 0){
			nbalert("打卡失败","请重新操作！");
		}else if(data == 1){
			//mbsimajax(APP+"/Member/mb_index","mbAjaxShow");
			//$("#getPoint").removeClass("ringIn").addClass("ringOut").attr("title","今天已经打过卡了,请明天再来。");
			nbalert("打卡成功，记得明天还要来哦。","超级大礼打卡送"+POINTBNAME);
		}else if(data == 2){
			nbalert("今天已经打过过卡啦，请明天再来。","超级大礼打卡送"+POINTBNAME);
		}else if(data == 3){
			nbalert("尚未手机验证，打卡失败!","超级大礼打卡送"+POINTBNAME);
		}else{
			nbalert("对不起,您尚未登录!","超级大礼打卡送"+POINTBNAME);
		} 
	});
}

//退出
function logout(){
	$.post(APP+"/User/logout",'',function(msg){
		if(msg){//先保留msg这个值以备以后使用
			client_clear();
		}
		location.href=REQUEST_URI;
	});
}

//退出
function client_clear(){
	delCookie('islogin');
	delCookie('mb_id');
	delCookie('isgetPointB');
	delCookie('isgetPointC');
}

//更新用户状态
function updateuser() {
	$.ajax({
		type: "POST",
		url:APP+"/User/ajaxcheckuser/",
		success: function(msg){
			if(msg==2){						//证明已经被封了或是没登陆
				alert("系统检测到您的帐号存在异常,可能是以下几种原因造成的: \r\r 1.因为路由器重新拨号改变了您的ip地址. \r 2.您在其它页面已经退出了登陆状态. \r 3.因为其它原因,你的ip地址产生了变动. \r 4.您的帐号被暂时冻结.\r\r请您不用担心,只要重新登陆就能解决这个问题.\r如果这个状态频繁出现并影响了您正常的竞拍,请发邮件到客服邮箱与我们联系!");
				logout();
			}else{
				setTimeout("updateuser()", 60000);//60S
			}
		}
	});
}



//加载登录信息
function loadloginbox(){
	addDialogbox("#ajaxSignin","登录",10001,502);
	//quitScreenMask();
	
	$.ajax({
		url: APP+"/User/signin/",
		cache: false,
		success: function(msg){
			$("#ajaxSignin .dialogboxContent").prepend(msg);
			var regMsg = '<p><a href="'+APP+'/User/register"><img src="'+IMG_PATH+'/member/regGetPoint.png" width="450" height="60" alt="现在注册即送300'+POINTBNAME+'" /></a></p>';
			$("#regMsg").attr("style","margin-top:10px;")
			$("#regMsg").prepend(regMsg);
			initStyle();
			if($("#userID").attr("value") == ""){
				$("#userID").focus();
			}else{
				$("#userPW").focus();
			}
		}
	});
}

//登录操作
function ajaxlogin() {
	var exp  = new Date();
	var codehtml='<label for="logincode">验证码:</label>'+'<input type="text" class="input fl space" id="logincode" name="logincode" style="width:60px;" maxlength="4" />'+'<img id="verifyImg_login" class="fl" src="'+APP+'/User/verify/'+exp.getTime()+'" onclick="fleshVerify(\'verifyImg_login\')" title="点击刷新验证码" style="width: 68px; height: 30px;"/>';
	var pass=$.formValidator.pageIsValid('loginform');
	var loginwrongtime=getCookie('loginwrongtime');
	
	if($("#fristloginmark").attr("value") == "F"){
		//_gaq.push(['_trackPageview','/stat_user/fristlogin']);//lee99加入google统计//第一次
	}

	if(pass){
		var queryString = $('#loginform').formSerialize();
		$.post(APP+"/User/ajaxlogin",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/ajaxlogin']);//lee99加入google统计
			if(msg==1){
				if(loginwrongtime>2){
					$('#verifycodeInput').removeClass("displayNone");
					$('#verifycodeInput').html(codehtml);
				}
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/wrongpwd']);//lee99加入google统计//错误密码
				showtips('邮箱名或密码不正确，请确认','tips',3);
			}else if(msg==2){
				$('#verifycodeInput').removeClass("displayNone");
				$('#verifycodeInput').html(codehtml);
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/wrongvlidcode']);//lee99加入google统计//错误验证码
				showtips('验证码有误，请重新输入','tips',3);
			}else{
				//_gaq.push(['_trackPageview','/stat_user/ajaxlogin/loginpass']);//lee99加入google统计//正常
				dologinurl();
			}
		});
	}
}

//注册
function addMember() {
	var userEmail=$("#userEmail").val();
	var userverifycode=$("#addMembercode").val();
	var pass=$.formValidator.pageIsValid('addMemberform');
	if(pass){
		$.post(APP+"/User/addmember",'mail='+userEmail+'&code='+userverifycode,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_registermail']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('注册成功，系统已发送验证邮件到您邮箱：'+userEmail+'里，如果5分钟内没有收到邮件请更换注册邮箱或重复发送邮件！','tips',1);
				showresendbtn();//让用户可以再次发送邮件
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}

//已有相同用户名的处理方式
function showresendbtn() {
	//已有相同的用户名
	$('#sendbtn').attr("style","display:none");
	$('#resendbtn').removeAttr("style");
}

//已有相同用户名的处理方式
function hideresendbtn() {
	//已有相同的用户名
	$('#resendbtn').attr("style","display:none");
	$('#sendbtn').removeAttr("style");
}

//重新发送邮件
function resendmail() {
	var userEmail=$("#userEmail").val();
	var userverifycode=$("#addMembercode").val();
	var pass=$.formValidator.pageIsValid('addMemberform');
	if(pass){
		$.post(APP+"/User/resendmail",'mail='+userEmail+'&code='+userverifycode,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/resend_registermail']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('系统已重新发送验证邮件到您邮箱：'+userEmail+'里，如果5分钟内没有收到邮件请更换注册邮箱或重复发送邮件！','tips',1);
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}

//找回密码
function ajaxgetpwd() {
	var pass=$.formValidator.pageIsValid('getpwdform');

	if(pass){
		var queryString = $('#getpwdform').formSerialize();
		$.post(APP+"/User/getpwd",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='0'){//先保留msg这个值以备以后使用
				showtips('操作成功，系统已发送邮件到您邮箱','tips',1);
			}else if(msg=='3'){//先保留msg这个值以备以后使用
				showtips('找回密码的功能每天只能用3次,请您稍后再试','tips',1);
			}else{
				showtips(msg,'tips',2);
			}
		});
	}
}

//判断是否在登陆页还是用ajax的形式登陆
function dologinurl()
{
	if(URL =='/User')
	{
		location.href=APP+'/Member/';
	}else{
		location.href=REQUEST_URI;
	}
}

//用户地址薄
function userAddress()
{
	addDialogbox("#userAddress","选择您的地址",10001,502);
	//quitScreenMask();
	$.ajax({
		url: APP+"/Member/userAddress/",
		cache: false,
		success: function(msg){
			$("#userAddress .dialogboxContent").html(msg);
			$("#userAddress .dialogboxContent").addClass("regWrap");
		}
	});
}

//用户地址薄
function selectaddr(id)
{
	//addDialogbox("#userAddress","选择您的地址",10001,502);
	//quitScreenMask();
	//alert($("#userRealName_"+id).text());
	$("#userRealName").val(
	$("#userRealName_"+id).text()
	);
	$("#userPhoneNumber").val(
	$("#userPhoneNumber_"+id).text()
	);
	$("#detailedAddress").val(
	$("#detailedAddress_"+id).text()
	);
	$("#zipCode").val(
	$("#zipCode_"+id).text()
	);
	$("#remarks").val(
	$("#remarks_"+id).text()
	);
	removeDialogbox("#userAddress");
}

//加载订单页面
function loadOrder(oid)
{
	addDialogbox("#order","订单",10001,860);
	var orderFrame = '<iframe id="orderFrame" onload="iframeAutoHeight(\'orderFrame\')" name="orderFrame" width="100%" scrolling="no" frameborder="no" allowtransparency="yes" src='+APP+'/Member/showOrder/id/'+oid+'></iframe>';
	$("#order .dialogboxContent").html(orderFrame);
}
//返点处理
function returnPoint(oid){
	if(confirm('是否返点？返点操作成功后将不能再进行补价购买！')){
		$.post(APP+'/Member/returnPoint',{id:oid},function(data){
			if(data=='1'){
				alert('操作成功，您本次竞拍的消费点已返回到您的帐户!');
				mbsimajax('/Member/mb_addgame/','mbAjaxShow');
			}else{
				alert('操作不成功，请再试一次或发邮件到客服邮箱与我们联系！，谢谢!');
			}
		});
	}
}
//加载订单页面
function loadGameOrder(oid)
{
	addDialogbox("#order","订单",10001,860);
	var orderFrame = '<iframe id="orderFrame" onload="iframeAutoHeight(\'orderFrame\')" name="orderFrame" width="100%" scrolling="no" frameborder="no" allowtransparency="yes" src='+APP+'/Member/showGameOrder/id/'+oid+'></iframe>';
	$("#order .dialogboxContent").html(orderFrame);
}

//选择图像
function loadUserIcon()
{
	$.ajax({
		url: APP+"/User/memberlogo/",
		cache: false,
		success: function(msg){
			$("#usericon .dialogboxContent").html(msg);
			$("#newIconList button").click(function(){
				loadUserIcon();
			});
		}
	});
}

//站外晒图操作
function winnerlink(tid)
{
	addDialogbox("#winersay","进行站外晒图",10001,860);
	$.ajax({
		url: APP+"/Member/mb_winnerlink/id/"+tid+"/",
		cache: false,
		type: "POST",
		success: function(msg){
			$("#winersay .dialogboxContent").html(msg);
		}
	});
}

//晒图操作及成功感言
function winnersay(tid)
{
	addDialogbox("#winersay","进行站内晒图及感言",10001,860);
	$.ajax({
		url: APP+"/Member/mb_winersay/id/"+tid+"/",
		cache: false,
		type: "POST",
		success: function(msg){
			$("#winersay .dialogboxContent").html(msg);
		}
	});
}
//晒图操作删除图片
function winnersay_delimg(id,tid)
{
	$.ajax({
		url: APP+"/Member/mb_winersay_delimg/id/"+id+"/",
		cache: false,
		type: "POST",
		success: function(msg){
			//alert('OK');
			simajax(APP+"/Member/mb_winersay_loadimg/id/"+tid+"/","picList");
		}
	});
}

//用户选择自己的图像
function usericon()
{
	addDialogbox("#usericon","选择自己的个性图像",10001,502);
	//quitScreenMask();
	loadUserIcon();
}

//用户选择自己的图像后操作
function selsectlogo(path){
	$('#showicon').attr('src',path);
	$('#photo').attr('value',path);
	removeDialogbox("#usericon");
}


function Weibo_tellme(){
	addDialogbox("#Weibo_tellme","闪拍一周年生日了,玩微博,拿奖励....",10002,702);
	msg='<div class="tc" >'+
		'<iframe scrolling="no" frameborder="0" border="0" id="sinaapi" src="'+APP+'/Member/sinaweibo_check/"  height="500"  width="700" ></iframe>'+
	'</div>';
	$("#Weibo_tellme .dialogboxContent").attr('style','padding:0px');
	$("#Weibo_tellme .dialogboxContent").html(msg);
}

function saveMessage(){
	var islogin;
	var mb_id;
	islogin=getCookie('islogin');
	mb_id=getCookie('mb_id');
	if(!islogin || !mb_id){
		loadloginbox();
	}else{
		var queryString = $('#messageForm').formSerialize();
		$.post(APP+"/Member/saveMessage",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='1' || msg=='0'){//先保留msg这个值以备以后使用
				location.href=APP+"/Message/index";
			}else{
				loadloginbox();
			}
		});
	}
}

function saveMessage2(){
	var islogin;
	var mb_id;
	islogin=getCookie('islogin');
	mb_id=getCookie('mb_id');
	if(!islogin || !mb_id){
		loadloginbox();
	}else{
		var queryString = $('#messageForm2').formSerialize();
		$.post(APP+"/Member/saveMessage",queryString,function(msg){
			//_gaq.push(['_trackPageview','/stat_user/send_resetpwd']);//lee99加入google统计
			if(msg=='1'){//先保留msg这个值以备以后使用
				nbalert('留言成功','tips',1);
				location.href=APP+"/Message/index";
			}else if(msg=='0'){
				nbalert('留言失败','tips',1);
			}else{
				loadloginbox();
			}
		});
	}
}

function indexlogin() {
	var queryString = $('#loginform').formSerialize();
	$.post(APP+"/User/indexlogin",queryString,function(msg){
		if(msg==1){
			showtips('邮箱名或密码不正确，请确认','tips',3);
		}else if(msg==2){
			showtips('邮箱名或密码输入有误，请重新输入','tips',3);
		}else{
			dologinurl();
		}
	});
}